package com.ysd.lis.controller.callNum;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.callNum.CallWindow;
import com.ysd.lis.service.callNum.CallBusinessService;
import com.ysd.lis.service.callNum.CallWindowService;
import com.ysd.lis.socket.MessageTemplate;
import com.ysd.lis.socket.WebSocketUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wh
 * @since 2025-05-28
 */
@RestController
@RequestMapping("/callNum/business")
public class CallBusinessController extends BaseController {

    @Autowired
    private CallBusinessService callBusinessService;
    @Autowired
    private CallWindowService callWindowService;

    @ApiOperation("排队取号")
    @GetMapping("/callNum")
    public Result callNum(String userName) {
        return callBusinessService.callNum(userName);
    }
    @ApiOperation("叫号")
    @GetMapping("/netNum")
    public Result netNum(@RequestParam("businessNo")String businessNo) {
        return callBusinessService.netNum(businessNo);
    }


    @ApiOperation("暂停/启用/开启/窗口")
    @GetMapping("/pauseWindow")
    public Result pauseWindow(@RequestParam("winCode")String winCode,@RequestParam("state") String state) {
        CallWindow callWindow = callWindowService.getOne(new LambdaQueryWrapper<CallWindow>().eq(CallWindow::getWinCode, winCode).eq(CallWindow::getDelFlag, 0));
        if("1".equals(state)){
            callWindow.setIsEnable(2);
        }else if("2".equals(state)){
            callWindow.setIsEnable(1);
        }else{
            callWindow.setIsEnable(1);
        }
        MessageTemplate messageTemplate = new MessageTemplate();
        messageTemplate.setMessage(winCode);
        if("1".equals(state)){
            messageTemplate.setType("pauseWindow");
        }else if("2".equals(state)){
            messageTemplate.setType("startWindow");
        }else{
            messageTemplate.setType("openWindow");
        }
        WebSocketUtils.sendAllMessage(messageTemplate);
        boolean b = callWindowService.updateById(callWindow);
        if (b) {
            return Result.succ(1, "暂停成功", "");
        } else {
            return Result.succ(400, "暂停失败", "");
        }
    }
    @ApiOperation("关闭窗口")
    @GetMapping("/closeWindow")
    public Result closeWindow(@RequestParam("winCode")String winCode) {
        return callBusinessService.closeWindow(winCode);

    }
}
